﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Morus;

namespace KAKA.Domain
{
    public partial class Member : IAggregateRoot
    {
        public static Member Create(User user)
        {
            var member = new Member
            {
                User = user,
                Id = user.Id,
            };

            member.Declaration = new Declaration
            {
                Member = member,
                Id = user.Id,
            };

            user.Member = member;

            return member;
        }

        public Album DefaultAlbum
        {
            get
            {
                if (this.Albums.FirstOrDefault() == null)
                {
                    EnsureDefaultAlbum();
                }
                return this.Albums.FirstOrDefault();
            }
        }

        public void EnsureDefaultAlbum()
        {
            this.Albums.Add(new Album
            {
                CreateAt = DateTime.Now,
                Id = Guid.NewGuid(),
                Member = this,
                Name = Resources.Label_1000_Default_Album,
            });
        }

        public void MakeFriend(Member friend)
        {
            if (this.Friendships.Any(r => r.Friend.Id == friend.Id) == false)
            {
                this.Friendships.Add(new Friendship
                {
                    Member = this,
                    Approved = false,
                    CreateAt = DateTime.Now,
                    Status = FriendshipStatus.Friendship,
                    Friend = friend,
                });
            }
            else
            {
                throw new Exception(Resources.Error_3000_Be_Friend_Already);
            }
        }

        public void Blacklist(Member enmity)
        {
            var friendship = this.Friendships.FirstOrDefault(r => r.Friend.Id == enmity.Id);
            if (friendship != null)
            {
                friendship.Status = FriendshipStatus.Blacklist;
                friendship.CreateAt = DateTime.Now;
            }
            else
            {
                this.Friendships.Add(new Friendship
                {
                    Member = this,
                    Approved = true,
                    CreateAt = DateTime.Now,
                    Status = FriendshipStatus.Blacklist,
                    Friend = enmity,
                });
            }
        }

        public IEnumerable<Member> Friends
        {
            get
            {
                return this.Friendships.Select(r => r.Friend).ToArray();
            }
        }

        public IEnumerable<Event> Events
        {
            get
            {
                return this.Slates.Select(r => r.Event).ToArray();
            }
        }

        public string Mobile
        {
            get
            {
                var crypto = IoC.Resolve<Cryptography>();
                var mobileBuff = Convert.FromBase64String(HashMobile);
                var saltBuff = Convert.FromBase64String(User.HashSalt);
                return crypto.Decrypt(mobileBuff, saltBuff);
            }
            set
            {
                var crypto = IoC.Resolve<Cryptography>();
                var saltBuff = Convert.FromBase64String(User.HashSalt);
                HashMobile = Convert.ToBase64String(crypto.Encrypt(value, saltBuff));
            }
        }
    }
}
